#include"map.h"
#include "torch.h"
#include "ui_torch.h"
#include<QPainter>
#include<QKeyEvent>
#include<QRandomGenerator>
#include<QPixmap>
#include<QtMath>
//#include<QMovie>
Torch::Torch(QWidget *parent)
    : QMainWindow(parent)
    ,isstart(true),isover(false), ui(new Ui::Torch),lives(6),direction(1),num(0)

{
    ui->setupUi(this);
    this->setGeometry(400,200,1000,600);

}

Torch::~Torch()
{
    delete ui;
}
//生成障碍
void Torch::becreat(Block& bl){

    starty=QRandomGenerator::global()->bounded(10);
    starts=QRandomGenerator::global()->bounded(2);
    switch(starty){
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:

        if(starts==0){
            bl.block.setRect(300+10*starty,180,20,20);
            bl.size=1;
            }

         if(starts==1){
            bl.block.setRect(300+10*starty,170,20,20);
            bl.size=2;
            }
        break;
    case 6:
    case 5:
    case 7:
    case 8:
    case 9:
        if(starts==0){
            bl.block.setRect(300+10*starty,200,20,15);
            bl.size=1;
            }

        if(starts==1){
            bl.block.setRect(300+10*starty,200,20,30);
            bl.size=2;
            }
        break;
    default:
        bl.block.setRect(300+8*starty,170,20,30);
        bl.size=2;

    }
}
//初始化人
void Torch::begin(){
    gravity=3;
    isjump=false;
    ten=20;
    pnum1=pnum2=ten;
    isstart=false;
    background=Map();
    {
        blocks.resize(10);
        becreat(blocks[0]);
        num=1;
    }
    person.setRect(250,180,10,19);
    timer1=startTimer(20);
    timer2=startTimer(1000);
    //timer3=startTimer(10);
    blood.resize(lives);
    for(int i=0;i<lives;i++){
        blood[i]=QRect(80+15*(i+1),45,15,4);
    }

}
void Torch::keyPressEvent(QKeyEvent*event){
    QKeyEvent *key=(QKeyEvent*)event;
    if(key->key()==Qt::Key_W)direction=1;
    if(key->key()==Qt::Key_S)direction=2;
    if(key->key()==Qt::Key_K&&person.bottom()<200&&isjump==false){
        direction=3;isjump=true;
        /*person.setTop(person.top()+vy);
        person.setBottom(person.bottom()+vy);*/
    }
}
void Torch::paintEvent(QPaintEvent*event){
    QPainter painter(this);


   /* painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(10,10,520,320);*/

    //painter.setPen(Qt::black);
    //QPixmap*image=new QPixmap();

    //painter.setBrush(Qt::white);
    //painter.drawRect(20,20,500,300);
    //painter.drawPixmap(0,0, QPixmap(":/image/images/shamotab"));
    painter.drawPixmap(background.cx1,0,background.map1);
    painter.drawPixmap(background.cx2,0,background.map2);
    painter.setPen(Qt::darkYellow);
    painter.drawLine(0,200,600,200);
    if(isstart)
        begin();
    painter.setBrush(Qt::black);
    painter.drawRect(person);
    /*painter.drawPixmap(person.left(),person.top(),10,19,QPixmap(":/image/images/runplayer(1)"));
    painter.drawPixmap(person.left(),person.top(),10,19,QPixmap(":/image/images/runplayer(2)"));
    painter.drawPixmap(person.left(),person.top(),10,19,QPixmap(":/image/images/runplayer(3)"));*/


    painter.setBrush(Qt::darkGreen);
    /*painter.drawRect(block);
    if(block.left()<=0||num==30){becreat(block);num=0;}*/
    for(int h=0;h<num;h++){
        //painter.drawRect(blocks[h].block);
        painter.drawPixmap(blocks[h].block.left(),blocks[h].block.top(),blocks[h].block.width(),blocks[h].block.height(),QPixmap(":/image/images/tack"));
    }
   if(isover)
    {
        killTimer(timer1);
        killTimer(timer2);
        //killTimer(timer3);
    }

    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::white);
    painter.drawText(50,50,"HP:");
    painter.drawText(190,50,QString::number(lives));
    painter.setBrush(Qt::red);
    if(!isover)
    painter.drawRects(&blood[0],blood.size());
    else ;
}

void Torch::picturemove(){
    background.mapmove();

    if(direction==1)person.setRect(250,179,10,20);
    if(direction==2)person.setRect(250,201,10,20);
    if(direction==3&&isjump==true){
    if(pnum1>0){jump();pnum1--;}
    if(pnum1==0&&pnum2>0){fall();pnum2--;}
    if(pnum1==0&&pnum2==0){pnum1=pnum2=ten;isjump=false;}
    }

    /*block.setLeft(block.left()-2);
block.setRight(block.right()-2);*/

    for(int f=0;f<num;f++)
    {
        blocks[f].block.setLeft(blocks[f].block.left()-2);
        blocks[f].block.setRight(blocks[f].block.right()-2);

        if(blocks[f].block.left()<=20){
            for(int m=0;m<num-1;m++){
                blocks[m]=blocks[m+1];
            }num-=1;
        }

    }
    if(ishit()){
        lives--;
        for(int m=*pos;m<num-1;m++){
            blocks[m]=blocks[m+1];
        }num-=1;
    }
    blood.resize(lives);
    ( lives>0)?isover=false:isover=true;
    update();
}

bool Torch::ishit(){
    for(int g=0;g<num;g++){
        int le=blocks[g].block.left();//int ri=blocks[g].block.right();
        int to=blocks[g].block.top();//int bo=blocks[g].block.bottom();
        int hei=blocks[g].block.height();int wi=blocks[g].block.width();
        if(abs(to+hei/2-(person.top()+person.height()/2))<(hei+person.height())/2&&abs(le+wi/2-(person.left()+person.width()/2))<(wi+person.width())/2)
        {
           // if(abs(le+wi/2-(person.left()+person.width()/2))<(wi+person.width())/2);
            {
                pos=&g;  return true;
            }
        }

    }return false;
}
void Torch::timerEvent(QTimerEvent *event) {
    int tem=event->timerId();
    if(tem == timer2) {
        if(num<blocks.size()){
            becreat(blocks[num]);
            num++;
        }
    }
    if(tem == timer1) {
        picturemove();
    }

}
void Torch::jump(){


            person.setTop(person.top()-gravity);
            person.setBottom(person.bottom()-gravity);



    }
void Torch::fall(){
            person.setTop(person.top()+gravity);
            person.setBottom(person.bottom()+gravity);
}
